package xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Created by qiao on 2017/1/8.
 */
public class XmlReder {

    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputStream is = new FileInputStream(new File("D:\\workspace2016_b\\ibatis\\src\\test\\resources\\test.xml"));
            Document doc = builder.parse(is);
            Element root = doc.getDocumentElement();
            NodeList books = root.getChildNodes();
            if(books != null){
                for(int i =0; i< books.getLength();i++){
                     Node book = books.item(i);
                     if(book.getNodeType() == Node.ELEMENT_NODE){
                         String email = book.getAttributes().getNamedItem("email").getNodeValue();
                         System.out.println(email);
                     }
                }
            }
        } catch (Exception e) {
             e.printStackTrace();
        }
    }
}
